package com.os.config;

import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * 创建es客户端
 * @author 风亦未止
 * @date 2023/5/3 15:08
 */
@Configuration
public class EsClientConfig extends AbstractElasticsearchConfiguration {

    @Value("${es.host}")
    private String host;

    /**
     * 代替TransportClient 创建es客户端
     * @return
     */
    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        ClientConfiguration build = ClientConfiguration.builder()
                .connectedTo(host).build();
        return RestClients.create(build).rest();
    }
}
